#include "queueserver.h"
/*
void *message_queue_server(void *arg)
{
    qserver *queue_pack = malloc(sizeof(qserver));
        if (!queue_pack) {
            perror("malloc error");
            //close(tcp_acceptsocketfd);
            return (void*)-1;
        }
        queue_pack->tcp_serverfd = ((qserver*)arg)->tcp_serverfd;
        queue_pack->pathname = ((qserver*)arg)->pathname;
        queue_pack->id =((qserver*)arg)->id;   
        free(arg);
         key_t key = ftok(queue_pack->pathname, queue_pack->id);
    int msgid = msgget(key, IPC_CREAT | 0666);
    if (msgid == -1) {
        perror("msgget failed");
        exit(1);
    }
    MsgQueue msg_buf;
    char temp_buff[1024];
    long expected_msgtype = 1; // 接收的消息类型为1
    while(1)
    { 
 
    // 使用msgrcv接收消息
        ssize_t bytes_read = msgrcv(msgid, (void*)&msg_buf, sizeof(msg_buf.data_len) + sizeof(msg_buf.data), expected_msgtype, 0);
        if (bytes_read == -1) {
            perror("msgrcv");
            return (void*)-1;
        }
        memset(temp_buff,'\0',1024);
        printf("msgrcv buff = %s\n",temp_buff);

        strncpy(temp_buff,msg_buf.data,msg_buf.data_len);
        write(queue_pack->tcp_serverfd,temp_buff,sizeof(temp_buff));

    }
}

int queueserver(int tcp_acceptsocketfd)
{
//    pthread_mutex_lock(&lock);//尝试上锁，阻塞判断tcp是否连接“对tcp_acceptsocketfd上锁”
//    pthread_mutex_unlock(&lock);//连接成功后解锁
    pthread_t tid;
    pthread_t tid2;
    qserver *queue_pack = malloc(sizeof(qserver));
        if (!queue_pack) {
            perror("malloc error");
            //close(tcp_acceptsocketfd);
            return -1;
        }
        queue_pack->tcp_serverfd = tcp_acceptsocketfd;
        queue_pack->pathname = QT_QUEUE_PATHNAME;
        queue_pack->id =QT_QUEUE_ID;
 
        if (pthread_create(&tid, NULL, message_queue_server, (void*)queue_pack) != 0) {
            perror("pthread_create error");
            free(queue_pack);
            //close(*tcp_acceptsocketfd);
             return -1;
        }
        queue_pack->tcp_serverfd = tcp_acceptsocketfd;
        queue_pack->pathname = ALI_QUEUE_PATHNAME;
        queue_pack->id =ALI_QUEUE_ID;
 
        if (pthread_create(&tid2, NULL, message_queue_server, (void*)queue_pack) != 0) {
            perror("pthread_create error");
            free(queue_pack);
            //close(*tcp_acceptsocketfd);
             return -1;
        }
 
        // 分离线程
        pthread_detach(tid);
        pthread_detach(tid2);

        return 0; 
}
*/
#include "queueserver.h"

void *message_queue_server(void *arg) {
    qserver *queue_pack = (qserver *)arg; // 直接转换类型，不需要再次malloc
    key_t key = ftok(queue_pack->pathname, queue_pack->id);
    int msgid = msgget(key, IPC_CREAT | 0666);
    if (msgid == -1) {
        perror("msgget failed");
        exit(1);
    }
//printf("msgget over\n");
    MsgQueue msg_buf;
    char temp_buff[1024];
    long expected_msgtype = 1;
    while (1) {
        ssize_t bytes_read = msgrcv(msgid, (void *)&msg_buf, sizeof(MsgQueue) - sizeof(long), expected_msgtype, 0);
        if (bytes_read == -1) {
            perror("msgrcv");
            return (void *)-1;
        }
        strncpy(temp_buff, msg_buf.data, msg_buf.data_len);
        //printf("strncpy over,strncpy = %s\n",temp_buff);
        temp_buff[msg_buf.data_len] = '\0'; // 确保字符串以null结尾
        write(queue_pack->tcp_serverfd, temp_buff, msg_buf.data_len);
    }
    return NULL; // 线程通常不会返回，但这里为了完整性
}

int queueserver(int tcp_acceptsocketfd,pthread_mutex_t *lock)
 {
    pthread_mutex_lock(lock);//尝试上锁，阻塞判断tcp是否连接“对tcp_acceptsocketfd上锁”
    pthread_mutex_unlock(lock);//连接成功后解锁
    //printf("start queueserver\n");
    pthread_t tid;
    qserver *queue_pack1 = malloc(sizeof(qserver));
    qserver *queue_pack2 = malloc(sizeof(qserver));
    if (!queue_pack1 || !queue_pack2) {
        perror("malloc error");
        free(queue_pack1);
        free(queue_pack2);
        return -1;
    }
//printf("malloc over\n");
    queue_pack1->tcp_serverfd = tcp_acceptsocketfd;
    queue_pack1->pathname = QT_QUEUE_PATHNAME;
    queue_pack1->id = QT_QUEUE_ID;

    if (pthread_create(&tid, NULL, message_queue_server, (void *)queue_pack1) != 0) {
        perror("pthread_create error");
        free(queue_pack1);
        return -1;
    }
//printf("QT pthread1 over\n ");
    queue_pack2->tcp_serverfd = tcp_acceptsocketfd; // 通常这里应该是另一个套接字，或者不应该共享
    queue_pack2->pathname = ALI_QUEUE_PATHNAME;
    queue_pack2->id = ALI_QUEUE_ID;

    pthread_t tid2;
    if (pthread_create(&tid2, NULL, message_queue_server, (void *)queue_pack2) != 0) {
        perror("pthread_create error");
        free(queue_pack2);
        return -1;
    }
//printf("QT pthread2 over\n ");
    pthread_detach(tid);
    pthread_detach(tid2);
    //printf("end queueserver\n");
    return 0;
}
